export default {
    inserted(el) {
      const observer = new IntersectionObserver((entries) => {
        entries.forEach(entry => {
          if (entry.isIntersecting) {
            // 处理所有需要懒加载的图片
            const images = el.querySelectorAll('img[data-src]');
            images.forEach(img => {
              // 添加加载类
              img.classList.add('lazy-loading');
              
              // 使用Image对象预加载
              const tempImg = new Image();
              tempImg.src = img.dataset.src;
              
              tempImg.onload = () => {
                img.src = img.dataset.src;
                img.classList.remove('lazy-loading');
                img.classList.add('lazy-loaded');
              };
              
              tempImg.onerror = () => {
                img.classList.remove('lazy-loading');
                img.classList.add('lazy-error');
                // 可以设置默认图片
                img.src = '';
              };
            });
            
            // 处理视频（如果有）
            const videos = el.querySelectorAll('video[data-src]');
            videos.forEach(video => {
              video.src = video.dataset.src;
              video.load();
            });
            
            observer.unobserve(el);
          }
        });
      }, {
        rootMargin: '200px', // 提前200px加载
        threshold: 0.01
      });
  
      observer.observe(el);
    }
  };